package cn.sail.shop3.api.dao.impl;

import cn.sail.shop3.api.dao.UserDao;
import cn.sail.shop3.api.entity.User;
import cn.sail.shop3.api.util.DBUtil;

import java.sql.*;

public class UserDaoImpl implements UserDao {
    @Override
    public User selectByUserName(String paramsUserName) {
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //连接数据库
            String url = "jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
            String jdbcUserName = "root";
            String password = "root";
            Connection connection = DriverManager.getConnection(url, jdbcUserName, password);
            //定义sql
            String sql = "SELECT * FROM `user` WHERE user_name='%s'";
            //参数拼接
            sql = String.format(sql, paramsUserName);
            System.out.println(sql);
            //获取发送执行sql对象工具
            PreparedStatement statement = connection.prepareStatement(sql);
            //执行sql,接收结果
            ResultSet resultSet = statement.executeQuery();
            //处理结果resultSet => User
            User dbUser = null;
            while (resultSet.next()) {
                dbUser = new User();
                long id = resultSet.getLong("id");
                dbUser.setId(id);
                String userName = resultSet.getString("user_name");
                dbUser.setUserName(userName);
                dbUser.setPassword(resultSet.getString("password"));
            }
            //释放资源
            resultSet.close();
            statement.close();
            connection.close();
            return dbUser;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public User selectById(Long id) {
        try {
            //定义sql
            String sql = "SELECT * FROM `user` WHERE id=?";
            Connection connection = DBUtil.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setLong(1, id);
            //执行sql,接收结果
            ResultSet resultSet = statement.executeQuery();
            //处理结果resultSet => User
            User dbUser = DBUtil.convertResultSetToEntity(resultSet, User.class);
            //释放资源
            DBUtil.closeAll(resultSet, statement, connection);
            return dbUser;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //约定优于配置

    @Override
    public int insert(User user) {
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //连接数据库
            String url = "jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
            String jdbcUserName = "root";
            String password = "root";
            Connection connection = DriverManager.getConnection(url, jdbcUserName, password);
            //定义sql
            String sql = "INSERT INTO `user`( `user_name`, `password`) VALUES (?, ?)";
            //获取发送执行sql对象工具
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUserName());
            statement.setString(2, user.getPassword());
            //执行sql,接收结果
            int result = statement.executeUpdate();
            //释放资源
            statement.close();
            connection.close();
            return result;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
